package pers.dawnyang.modular.system.controller;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pers.dawnyang.common.annotation.OperLog;
import pers.dawnyang.common.domain.Result;
import pers.dawnyang.common.domain.entity.SysDictType;
import pers.dawnyang.common.domain.entity.SysDictValue;
import pers.dawnyang.common.enums.OperType;
import pers.dawnyang.common.util.ResultUtil;
import pers.dawnyang.config.cache.DictUtils;
import pers.dawnyang.modular.system.domain.request.dictValue.DictValueReqC;
import pers.dawnyang.modular.system.domain.request.dictValue.DictValueReqU;
import pers.dawnyang.modular.system.service.DictTypeService;
import pers.dawnyang.modular.system.service.DictValueService;

/**
 * 字典值管理
 * 
 * @author dawn yang
 * 
 */

@RequestMapping("/sys/dictValue")
@Controller
@Validated
public class DictValueController {

  @Autowired
  private DictValueService dictValueService;

  @Autowired
  private DictTypeService dictTypeService;

  @RequiresPermissions("sys:dict:add")
  @GetMapping("/addView")
  public String addView(@RequestParam("typeCode") String typeCode, ModelMap model) {
    model.put("typeCode", typeCode);
    return "/sys/dictValue/addView";
  }

  @RequiresPermissions("sys:dict:edit")
  @GetMapping("/editView")
  public String editView(@RequestParam("id") Long id, ModelMap model) {
    SysDictValue sysDictValue = dictValueService.getById(id);
    model.put("model", JSON.toJSONString(sysDictValue));
    return "/sys/dictValue/editView";
  }

  @RequiresPermissions("sys:dict:list")
  @PostMapping("/getPage")
  @ResponseBody
  public Result<Page<SysDictValue>> getPage(Page<SysDictValue> page, SysDictValue req) {
    LambdaQueryWrapper<SysDictValue> lqw = new LambdaQueryWrapper<>();
    lqw.eq(SysDictValue::getTypeCode, req.getTypeCode());
    lqw.like((StringUtils.isNotBlank(req.getValueCode())), SysDictValue::getValueCode,
        req.getValueCode());
    lqw.like((StringUtils.isNotBlank(req.getValueName())), SysDictValue::getValueName,
        req.getValueName());
    lqw.eq((null != req.getStatus()), SysDictValue::getStatus, req.getStatus());
    lqw.orderByAsc(SysDictValue::getSort);
    Page<SysDictValue> pageRes = dictValueService.page(page, lqw);
    return ResultUtil.success(pageRes);
  }

  @OperLog(operModul = "字典管理", operType = OperType.UPDATE)
  @RequiresPermissions("sys:dict:edit")
  @PostMapping("/edit")
  @ResponseBody
  public Result<String> edit(@Validated DictValueReqU req) {
    // SysDictValue res = dictValueService.getById(req.getValueId());
    // DictUtils.clearByTypeCode(res.getTypeCode());
    SysDictValue sysDictValue = new SysDictValue();
    BeanUtils.copyProperties(req, sysDictValue);
    dictValueService.updateById(sysDictValue);
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

  @OperLog(operModul = "字典管理", operType = OperType.INSERT)
  @RequiresPermissions("sys:dict:add")
  @PostMapping("/add")
  @ResponseBody
  public Result<String> add(@Validated DictValueReqC req) {
    LambdaQueryWrapper<SysDictType> lqw = new LambdaQueryWrapper<>();
    lqw.like((StringUtils.isNotBlank(req.getTypeCode())), SysDictType::getTypeCode,
        req.getTypeCode());
    List<SysDictType> list = dictTypeService.list(lqw);
    if (null == list || list.size() == 0) {
      return ResultUtil.error();
    }
    SysDictValue sysDictValue = new SysDictValue();
    BeanUtils.copyProperties(req, sysDictValue);
    dictValueService.save(sysDictValue);
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

  @OperLog(operModul = "字典管理", operType = OperType.DELETE)
  @RequiresPermissions("sys:dict:del")
  @PostMapping("/del")
  @ResponseBody
  public Result<String> del(@RequestParam("id") Long id) {
    SysDictValue res = dictValueService.getById(id);
    DictUtils.clearByTypeCode(res.getTypeCode());
    SysDictValue sysDictValue = new SysDictValue();
    sysDictValue.setValueId(id);
    sysDictValue.setStatus(0);
    dictValueService.updateById(sysDictValue);
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

}
